package com.sense.cloud.coreservice.sdk.auth.utils;

import com.alibaba.fastjson.JSON;
import com.sense.cloud.coreservice.sdk.auth.utils.assist.AuthModel;
import com.sense.cloud.coreservice.sdk.auth.utils.assist.Permissions;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

public class SdkPropUtil {
  private static final Logger LOGGER = LoggerFactory.getLogger(SdkPropUtil.class);
  private static final String FILENAME = "authsdk.properties";
  private static final String JSONFILE = "authsdk.json";
  private static Properties prop = new Properties();
  private static AuthModel model = null;

  static {
    ClassLoader loader = SdkPropUtil.class.getClassLoader();
    InputStream in = null;
    InputStream is = null;
    try {
      in = loader.getResourceAsStream(FILENAME);
      prop.load(in);
      is = loader.getResourceAsStream(JSONFILE);
      String jsonStr = IOUtils.toString(is);
      model = JSON.parseObject(jsonStr, AuthModel.class);
    } catch (IOException e11) {
      LOGGER.error("Load " + FILENAME + " failed!", e11);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e22) { 
          //
        }
      }
      if (is != null) {
        try {
          is.close();
        } catch (IOException e33) {
          //
        }
      }
    }
  }

  private SdkPropUtil() {
  }

  /**
   * 
   * @param key.
   * @param defultVal.
   * @return str.
   */
  public static String getStr(String key, String defultVal) {
    String val = prop.getProperty(key);
    if (val == null || val.length() == 0) {
      val = defultVal;
    }
    return val;
  }
  
  /**
   * 
   * @param key.
   * @param defultVal.
   * @return str.
   */
  public static String getProperty(String key, String defultVal) {
    String val = prop.getProperty(key);
    if (val == null || val.length() == 0) {
      val = defultVal;
    }
    return val;
  }

  /**
   * 
   * @param key.
   * @param defultVal.
   * @return str.
   */
  public static Integer getInt(String key, Integer defultVal) {
    String val = prop.getProperty(key);
    Integer intval = defultVal;
    if (val != null && val.length() > 0) {
      intval = Integer.parseInt(val);
    }
    return intval;
  }

  /**
   * 
   * @return str.
   */
  public static String getSsoPrikeyAesKey() {
    return getStr("sso.prikey.aes.key", 
      "c6d9499da30b4022256b1283ee6befc3825ef48af267559d4c7f99ac44932aba");
  }

  /**
   * 
   * @return int.
   */
  public static Integer getHttpConnectionPoolMax() {
    int defaultMax = 1000;
    try {
      defaultMax = getInt("http.connection.pool.max", defaultMax);
    } catch (Exception e11) {
      LOGGER.error(e11.getMessage(), e11);
    }
    return defaultMax;
  }

  
  
  /**
   * @param accessKey.
   * @return str.
   */
  public static String getAppnameFromMap(String accessKey) {
    List<AuthModel>  list = model.getAllowedAccessApps();
    if ( list != null && list.size() > 0 ) {
      for (AuthModel a : list) {
        if ( a != null && a.getAccesskey().equals(accessKey) ) {
          return a.getAppname();
        }
      }
    }
    return null;
  }
  
  /**
   * @param accessKey.
   * @return str
   */
  public static String getSecretFromMap(String accessKey) {
    if ( Objects.deepEquals(accessKey, model.getAccesskey()) ) {
      return model.getSecret();
    }
    
    List<AuthModel>  list = model.getAllowedAccessApps();
    if ( list != null && list.size() > 0 ) {
      for (AuthModel a : list) {
        if ( a != null && a.getAccesskey().equals(accessKey) ) {
          return a.getSecret();
        }
      }
    }
    return null;
  }
  
  /**
   * @return str.
   */
  public static String getAppname() {
    //String accessKey = getStr("auth.accesskey", "1");
    //return accessKey;
    return model.getAppname();
  }

  /**
   * 
   * @return str.
   */
  public static String getAccessKey() {
    //String accessKey = getStr("auth.accesskey", "1");
    //return accessKey;
    return model.getAccesskey();
  }

  public static String getSecret() {
    //String secret = SdkPropUtil.getStr("auth.secret", "1");
    return model.getSecret();
  }
  
  /**
   * @param accessKey.
   * @return str.
   */
  public static List<String> getAccesskeysFromCode(String code) {
    List<Permissions> permissions = model.getPermissions();
    if ( permissions != null && permissions.size() > 0 ) {
      for (Permissions ps : permissions) {
        if ( ps != null && Objects.deepEquals(ps.getCode(), code)) {
          return ps.getAccesskeys();
        }
      }
    }
    return null;
  }

}
